// Device/pages/Bind/Verify/components/existence-component/index.js

Component({

    /**
     * 组件的属性列表
     */
    properties: {
        componentData: {
            default: {},
            type: Object
        }
    },
    observers: {
        'componentData': function (value) {
            console.log(value)
            for(let key in value) {
                if(key == 'device_sensitivity') {
                    value['device_sensitivity_label'] = value[key];
                }
                if(key == 'dormancy_times' && value[key]) {
                    value['dormancy_start_times'] = value[key].split('-')[0];
                    value['dormancy_end_times'] = value[key].split('-')[1];
                }
                if(key == 'detection_range') {
                    value['detection_range_label'] = String(Number(value[key]).toFixed(0));
                }
            }
            if (value) {
                this.setData({
                    ...value
                })
            }
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        minHour: 0,
        maxHour: 23,
        currentDate: '00:00',
        show: false,
        columns: [],
        pickerType: 'detection_range',
        timeShow: false,
        timeType: 'start',
        unattended_alarm_status: false,
        unattended_alarm_time: null,
        holdup_alarm_status: false,
        holdup_alarm_time: null,
        dormancy_status: false,
        dormancy_times: null,
        dormancy_start_times: '',
        dormancy_end_times: '',
        pilot_lamp_status: false,
        delay_time: null,
        detection_range: null,
        detection_range_label: null,
        device_sensitivity: null,
        device_sensitivity_label: null,
        device_location: null
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onChange(e) {
            this.setData({
                [`${e.currentTarget.dataset.type}`]: !this.data[`${e.currentTarget.dataset.type}`]
            })
        },
        onPickerTap(e) {
            const {
                type
            } = e.currentTarget.dataset;
            this.setData({
                show: true,
                pickerType: type,
                columns: type == 'detection_range' ? [{
                    key: '1',
                    value: '1'
                }, {
                    key: '2',
                    value: '2'
                }, {
                    key: '3',
                    value: '3'
                }, {
                    key: '4',
                    value: '4'
                }, {
                    key: '5',
                    value: '5'
                }, {
                    key: '6',
                    value: '6'
                }] : type == 'device_sensitivity' ? [{
                    key: '低',
                    value: '低'
                }, {
                    key: '中',
                    value: '中'
                }, {
                    key: '高',
                    value: '高'
                }] : type == 'holdup_alarm_time' ? [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12].map(item => {
                    return {
                        key: item,
                        value: item
                    }
                }) : [1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10.5, 11, 11.5, 12].map(item => {
                    return {
                        key: item,
                        value: item
                    }
                })
            })
        },
        onPickerConfirm(e) {
            this.setData({
                [`${this.data.pickerType}_label`]: e.detail.value.value,
                [`${this.data.pickerType}`]: e.detail.value.key,
                show: false
            })
        },
        onPickerCancel() {
            this.setData({
                show: false
            })
        },
        onTimeCancel() {
            this.setData({
                timeShow: false
            })
        },
        onTimeConfirm(e) {
            this.setData({
                [`dormancy_${this.data.timeType}_times`]: e.detail,
                timeShow: false
            })
        },
        onTimeTap(e) {
            this.setData({
                timeShow: true,
                timeType: e.currentTarget.dataset.type
            })
        },
        onNavigate() {
            this.triggerEvent('navigate')
        }
    }
})